/**
 * VideoStudyRecordService.java
 * com.sftj.datacollect.analytics.service
 * Copyright (c) 2018, 北京科技有限公司版权所有.
*/

package com.sftz.modules.datacollect.analytics.service.Impl;

import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;

import org.nutz.json.Json;

import com.google.common.collect.Lists;
import com.google.common.io.Resources;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sftz.framework.vendors.elasticsearch.EsSearchUtil;
import com.sftz.modules.datacollect.analytics.dto.RecordBehaviorLogDto;
import com.sftz.modules.datacollect.analytics.service.StudyRecordVService;
import com.sftz.modules.datacollect.dto.StudyVideoImportDto;
import com.sftz.modules.datacollect.dto.VideoStudyDto;
import com.uxuexi.core.common.util.Util;

public class VideoStudyRecordServiceImpl implements StudyRecordVService {

	/**
	 * 获取用户的学习视频的总秒数
	 * @Date 2018年5月23日
	 * @author gyp
	 * @param courseId
	 * @param userId
	 */
	@Override
	public List<VideoStudyDto> getVideoStudyInfo(int courseId, int userId) {
		//		Sql sql = Sqls.create(sqlManager.get("web_get_video_study_info"));
		//		sql.setParam("courseId", courseId);
		//		sql.setParam("userId", userId);
		//		List<VideoStudyDto> lists = DbSqlUtil.query(dbDao, VideoStudyDto.class, sql);
		List<VideoStudyDto> lists = Lists.newArrayList();
		try {
			String indexName = "logstash-study-*";
			String tmplPath = "elasticsearch/templates/log_video_record_query.js";
			URL url = Resources.getResource(tmplPath);
			String query = Resources.toString(url, Charset.forName("utf-8"));
			query = query.replaceAll("@courseId", courseId + "");
			query = query.replaceAll("@userId", userId + "");
			List<RecordBehaviorLogDto> lst = EsSearchUtil.searchTmpl(query, indexName, RecordBehaviorLogDto.class);
			String listStr = Json.toJson(lst);
			Gson gs = new Gson();
			List<RecordBehaviorLogDto> jsonListObject = gs.fromJson(listStr,
					new TypeToken<List<RecordBehaviorLogDto>>() {
					}.getType());//把JSON格式的字符串转为List  
			VideoStudyDto videoStudy = null;
			if (jsonListObject.size() > 0) {
				for (RecordBehaviorLogDto ubl : jsonListObject) {
					StudyVideoImportDto svid = gs.fromJson(Json.toJson(ubl.getRequest_params()),
							StudyVideoImportDto.class);
					if (!Util.isEmpty(svid)) {
						videoStudy = new VideoStudyDto();
						videoStudy.setCourseId(Integer.parseInt(svid.getCourseId()));
						videoStudy.setTime(Integer.parseInt(svid.getTime()));
						videoStudy.setUserId(ubl.getRemote_user());
						videoStudy.setVideoId(svid.getVid());
						lists.add(videoStudy);
					}
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return lists;
	}

	/**
	 * 
	 * @Date 2018年6月1日
	 * @author gyp
	 * @param courseId
	 * @param userId
	 * @param vid
	 * @return(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	@Override
	public VideoStudyDto getVideoStudyRecordLastInfo(int courseId, int userId, String vid) {
		//		Sql sql = Sqls.create(sqlManager.get("web_get_video_study_lastInfo"));
		//		sql.setParam("courseId", courseId);
		//		sql.setParam("userId", userId);
		//		sql.setParam("videoId", vid);
		//		VideoStudyDto dto = DbSqlUtil.fetchEntity(dbDao, VideoStudyDto.class, sql);
		try {

			String indexName = "logstash-study-*";
			String tmplPath = "elasticsearch/templates/log_video_record_last_query.js";
			URL url = Resources.getResource(tmplPath);
			String query = Resources.toString(url, Charset.forName("utf-8"));
			query = query.replaceAll("@size", "1");
			query = query.replaceAll("@courseId", courseId + "");
			query = query.replaceAll("@userId", userId + "");
			query = query.replaceAll("@vid", vid);
			VideoStudyDto videoStudy = null;
			List<RecordBehaviorLogDto> lst = EsSearchUtil.searchTmpl(query, indexName, RecordBehaviorLogDto.class);
			if (lst.size() > 0) {
				String listStr = Json.toJson(lst.get(0));
				Gson gs = new Gson();
				RecordBehaviorLogDto recordBehaviorLogDto = gs.fromJson(listStr, RecordBehaviorLogDto.class);
				StudyVideoImportDto svid = gs.fromJson(Json.toJson(recordBehaviorLogDto.getRequest_params()),
						StudyVideoImportDto.class);
				if (!Util.isEmpty(svid)) {
					videoStudy = new VideoStudyDto();
					videoStudy.setCourseId(Integer.parseInt(svid.getCourseId()));
					videoStudy.setTime(Integer.parseInt(svid.getTime()));
					videoStudy.setUserId(recordBehaviorLogDto.getRemote_user());
					videoStudy.setVideoId(svid.getVid());
					return videoStudy;
				}
			}
			return null;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

	}
}
